| PIZZA | Current compiler version: 0.39d |
| A substantial companion to Java | |
| Introduction to Java and Pizza | |
|
Contents
Home Mirrors FAQ Distribution
|
all variables need to be declared
Local variables visible from their point of declaration until end of block
Class fields and methods visible everywhere in class
IOException myFault = new IOException();
throw myFault;
throw new IOException("write error");
Exceptions can be "caught" by a try statement. General form:
try {
...
} catch (Exception1 ex) {
...
} catch (Exception2 ex) {
...
}
Catch clauses are tried in sequence. First clause that matches the thrown exception is executed.
An optional finally section in the try statement contains code that will be executed whether an exception was raised or not.
Example
try {
out.write(...);
} finally {
out.close();
}
This is equivalent to:
try {
out.write(...);
out.close();
} catch (Throwable ex) {
out.close();
throw ex;
}
Uncaught exceptions have to be declared in a method signature:
void write2(OutputStream out, int x)
throws IOEception {
out.write(x); out.write(x);
}
Only exceptions: Exceptions inheriting from RuntimeException or Error
Throwable -+--- Exception -+---- ...
| |
| +--RuntimeException
+--- Error
group . project . class company . library . class
pizza.compiler.Main
java.lang.System
package myGroup.myPackage;
packagename . classname
The package of a class influences where the class is searched.
Interpreter and compiler will look for a class such as pizza.compiler.Main in the directory pizza/compiler/Main.
The root of the search is a directory on the CLASSPATH.
Setting the compiler's -d option will alsways ensure that classes end up in the right directory.
| import java.io.File | lets you use File instead of java.io.File |
| import java.io.* | makes available all files in java.io in unqualified form. |
Organized as packages:
| java.lang | Basis classes: Object, String, System, Math |
| java.io | Input/Output |
| java.util | Utility classes: hash tables, vectors, enumerations |
| java.applet | Helper classes for applet execution |
| java.awt | Platform-independent graphical user interface |
| java.net | Network programming |
class X extends Superclass [implements Interface ...]
class X extends Y { ...
X anX; Y aY;
aY = anX; // OK;
anX = (X)aY; // explicit type conversion required.
class String {
...
boolean equals(Object other) {
if (other instanceof String) {
String that = (String)other;
if (this.len != that.len) return false;
for (int i = 0; i < this.len; i++) {
if (this.chars[i] != that.chars[i])
return false;
return true;
}
}
String[] a = {"hello", "world!"};
Object[] b = a; // OK because of array subtyping rule
b[0] = new Integer(1); // will throw an ArrayStoreException ...
String s = a[0]; // ... to avoid having to assign an Integer
// to a String
class Base {
print(Object x) {
System.out.println(x);
}
printArray(Object[] xs) {
for (int i = 0; i < xs.length; i++) print(xs[i]);
}
}
class Sub extends Base {
PrintStream log = ...;
print(Object x) / overrides Base.print {
log.println(x);
}
}
Base x = new Sub();
x.print("hello") // will print to log
String toString();
boolean equals(Object other);
int hashCode();
void print(String x) { ... }
void print(Exception x) { ... }
void print(int x) { ... }
void print(int x, int precision) { ... }
| public |
visible everywhere |
| protected |
visible in all subclasses and in same package |
| (none) |
visible in same package |
| private |
visible only in same class |
abstract method print();
| abstract |
|
| public |
|
| final |
|
interface Storable {
int READ = 0;
int WRITE = 1;
byte[] get();
put(byte[] data);
int lastOp(); // returns READ or WRITE; }
interface Colors {
int Red = 0;
int Green = Red + 1;
int Blue = Green + 1;
}
class C implements /* imports, really */ Colors
public interface Set {
Set insert(Object elem);
Set delete(Object elem);
boolean member(Object elem);
}
abstract public class List implements Set {
abstract Set insert(Object elem);
abstract Set delete(Object elem);
abstract boolean member(Object elem);
}
public class Empty extends List {
NonEmpty(Object head; List rest) {
this.head = head;
this.rest = rest;
}
Set insert(Object elem) {
return new NonEmpty(elem, this);
}
Set delete(Object elem) {
return this;
}
boolean member(Object elem) {
return false;
}
}
An interface for Tables is given as follows:
interface Table {
void put(Object key, Object data);
Object get(Object key);
}